﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using V5_Model;

namespace V5_DAL
{
    public class DALArticle
    {
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ID", "K_Article");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from K_Article");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelArticle model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into K_Article(");
            strSql.Append("ID,ChannelID,Title,KeyWords,ClassID,CopyFrom,Author,Editor,Content,ShortContent,IsUrl,LinkUrl,DefaultPicUrl,Hits,CutomField,Passed,AddDateTime,UpDateTime)");
            strSql.Append(" values (");
            strSql.Append("@ID,@ChannelID,@Title,@KeyWords,@ClassID,@CopyFrom,@Author,@Editor,@Content,@ShortContent,@IsUrl,@LinkUrl,@DefaultPicUrl,@Hits,@CutomField,@Passed,@AddDateTime,@UpDateTime)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@ChannelID", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.VarChar,255),
					new SqlParameter("@KeyWords", SqlDbType.VarChar,255),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@CopyFrom", SqlDbType.VarChar,255),
					new SqlParameter("@Author", SqlDbType.VarChar,50),
					new SqlParameter("@Editor", SqlDbType.VarChar,50),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@ShortContent", SqlDbType.Text),
					new SqlParameter("@IsUrl", SqlDbType.Int,4),
					new SqlParameter("@LinkUrl", SqlDbType.VarChar,255),
					new SqlParameter("@DefaultPicUrl", SqlDbType.VarChar,255),
					new SqlParameter("@Hits", SqlDbType.Int,4),
					new SqlParameter("@CutomField", SqlDbType.Text),
					new SqlParameter("@Passed", SqlDbType.Int,4),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime),
					new SqlParameter("@UpDateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.ChannelID;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.KeyWords;
            parameters[4].Value = model.ClassID;
            parameters[5].Value = model.CopyFrom;
            parameters[6].Value = model.Author;
            parameters[7].Value = model.Editor;
            parameters[8].Value = model.Content;
            parameters[9].Value = model.ShortContent;
            parameters[10].Value = model.IsUrl;
            parameters[11].Value = model.LinkUrl;
            parameters[12].Value = model.DefaultPicUrl;
            parameters[13].Value = model.Hits;
            parameters[14].Value = model.CutomField;
            parameters[15].Value = model.Passed;
            parameters[16].Value = model.AddDateTime;
            parameters[17].Value = model.UpDateTime;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelArticle model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update K_Article set ");
            strSql.Append("ChannelID=@ChannelID,");
            strSql.Append("Title=@Title,");
            strSql.Append("KeyWords=@KeyWords,");
            strSql.Append("ClassID=@ClassID,");
            strSql.Append("CopyFrom=@CopyFrom,");
            strSql.Append("Author=@Author,");
            strSql.Append("Editor=@Editor,");
            strSql.Append("Content=@Content,");
            strSql.Append("ShortContent=@ShortContent,");
            strSql.Append("IsUrl=@IsUrl,");
            strSql.Append("LinkUrl=@LinkUrl,");
            strSql.Append("DefaultPicUrl=@DefaultPicUrl,");
            strSql.Append("Hits=@Hits,");
            strSql.Append("CutomField=@CutomField,");
            strSql.Append("Passed=@Passed,");
            strSql.Append("AddDateTime=@AddDateTime,");
            strSql.Append("UpDateTime=@UpDateTime");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@ChannelID", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.VarChar,255),
					new SqlParameter("@KeyWords", SqlDbType.VarChar,255),
					new SqlParameter("@ClassID", SqlDbType.Int,4),
					new SqlParameter("@CopyFrom", SqlDbType.VarChar,255),
					new SqlParameter("@Author", SqlDbType.VarChar,50),
					new SqlParameter("@Editor", SqlDbType.VarChar,50),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@ShortContent", SqlDbType.Text),
					new SqlParameter("@IsUrl", SqlDbType.Int,4),
					new SqlParameter("@LinkUrl", SqlDbType.VarChar,255),
					new SqlParameter("@DefaultPicUrl", SqlDbType.VarChar,255),
					new SqlParameter("@Hits", SqlDbType.Int,4),
					new SqlParameter("@CutomField", SqlDbType.Text),
					new SqlParameter("@Passed", SqlDbType.Int,4),
					new SqlParameter("@AddDateTime", SqlDbType.DateTime),
					new SqlParameter("@UpDateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.ChannelID;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.KeyWords;
            parameters[4].Value = model.ClassID;
            parameters[5].Value = model.CopyFrom;
            parameters[6].Value = model.Author;
            parameters[7].Value = model.Editor;
            parameters[8].Value = model.Content;
            parameters[9].Value = model.ShortContent;
            parameters[10].Value = model.IsUrl;
            parameters[11].Value = model.LinkUrl;
            parameters[12].Value = model.DefaultPicUrl;
            parameters[13].Value = model.Hits;
            parameters[14].Value = model.CutomField;
            parameters[15].Value = model.Passed;
            parameters[16].Value = model.AddDateTime;
            parameters[17].Value = model.UpDateTime;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from K_Article ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelArticle GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,ChannelID,Title,KeyWords,ClassID,CopyFrom,Author,Editor,Content,ShortContent,IsUrl,LinkUrl,DefaultPicUrl,Hits,CutomField,Passed,AddDateTime,UpDateTime from K_Article ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = ID;

            ModelArticle model = new ModelArticle();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ChannelID"].ToString() != "")
                {
                    model.ChannelID = int.Parse(ds.Tables[0].Rows[0]["ChannelID"].ToString());
                }
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.KeyWords = ds.Tables[0].Rows[0]["KeyWords"].ToString();
                if (ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                model.CopyFrom = ds.Tables[0].Rows[0]["CopyFrom"].ToString();
                model.Author = ds.Tables[0].Rows[0]["Author"].ToString();
                model.Editor = ds.Tables[0].Rows[0]["Editor"].ToString();
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                model.ShortContent = ds.Tables[0].Rows[0]["ShortContent"].ToString();
                if (ds.Tables[0].Rows[0]["IsUrl"].ToString() != "")
                {
                    model.IsUrl = int.Parse(ds.Tables[0].Rows[0]["IsUrl"].ToString());
                }
                model.LinkUrl = ds.Tables[0].Rows[0]["LinkUrl"].ToString();
                model.DefaultPicUrl = ds.Tables[0].Rows[0]["DefaultPicUrl"].ToString();
                if (ds.Tables[0].Rows[0]["Hits"].ToString() != "")
                {
                    model.Hits = int.Parse(ds.Tables[0].Rows[0]["Hits"].ToString());
                }
                model.CutomField = ds.Tables[0].Rows[0]["CutomField"].ToString();
                if (ds.Tables[0].Rows[0]["Passed"].ToString() != "")
                {
                    model.Passed = int.Parse(ds.Tables[0].Rows[0]["Passed"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDateTime"].ToString() != "")
                {
                    model.AddDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddDateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpDateTime"].ToString() != "")
                {
                    model.UpDateTime = DateTime.Parse(ds.Tables[0].Rows[0]["UpDateTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ChannelID,Title,KeyWords,ClassID,CopyFrom,Author,Editor,Content,ShortContent,IsUrl,LinkUrl,DefaultPicUrl,Hits,CutomField,Passed,AddDateTime,UpDateTime ");
            strSql.Append(" FROM K_Article ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,ChannelID,Title,KeyWords,ClassID,CopyFrom,Author,Editor,Content,ShortContent,IsUrl,LinkUrl,DefaultPicUrl,Hits,CutomField,Passed,AddDateTime,UpDateTime ");
            strSql.Append(" FROM K_Article ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "K_Article";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }

        #endregion  成员方法
    }
}
